﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using NetJsWire.JsonRpcHandler.Attributes;
using NetJsWire.Widgets.Core;
using NetJsWire.Widgets.Renderers.Core;
using NetJsWire.Widgets.Ui.Layout;

namespace NetJsWire.Widgets.Renderers.Form
{
    [ClassName( "Ext.ux.NTextField" )]
    internal class TextField : Widget
    {
        //protected string _INPUT = "{0}.on(\"change\", function(tf, newValue, oldValue) {{SendToServer(\"{0}\", \"input\", \"{1}\", [newValue, tf.isValid()]);}});";
        // ReSharper disable InconsistentNaming
        protected const string _INPUT_CLIENT_NAME = "input";
        protected string _INPUT = string.Empty;
        protected string _TEXTCHANGED = string.Empty;
        // ReSharper restore InconsistentNaming

        public TextField( object baseObject )
            : base( baseObject )
        {
        }

        public override void Render()
        {
            if ( !Rendered )
            {
                if ( BaseParent is IWidgetContainer )
                {
                    SetAttribute( ControlNode, "isInContainer", true );
                    SetConstructionProperty( "isInContainer", true );

                    if ( BaseParent is Ui.Form.FormPanel || BaseParent is Ui.Form.FieldSet )
                    {
                        if ( ( BaseParent as IWidgetContainer ).Layout == LayoutType.Form )
                            SetConstructionProperty( "fieldLabel", Base.ConfigOptions.FieldLabel ?? Base.FieldLabel );
                    }
                }
                else
                {
                    SetStyleProperty( "position", "absolute" );
                    SetConstructionProperty( "renderTo", ClientParentToRender );
                }

                EnsureParentNodePosition();

                ConstructorParameters = "\"" + ClientID + "\"";

                if ( Base.Multiline )
                {
                    ClassName = "Ext.ux.NTextArea";
                    if ( Base.ConfigOptions.Xtype == "ntextfield" )
                        Base.ConfigOptions.Xtype = "ntextarea";
                    SetConstructionProperty( "height", Base.ClientSize.Height );
                }

                if ( !string.IsNullOrEmpty( Base.Text ) )
                    SetConstructionProperty( "value", Base.Text );
                //SetConstructionProperty( "width", Base.Width );
                SetConstructionProperty( "tabIndex", Base.TabIndex );
                //SetConstructionProperty( "disabledClass", "disabled-tf" );

                if ( !Base.PasswordChar.Equals( '\0' ) )
                    SetConstructionProperty( "inputType", "password" );

                if ( !( Base is Ui.Form.NumberField ) && !string.IsNullOrEmpty( Base.Mask ) )
                    Plugins.Add( string.Format( "new Ext.ux.InputTextMask('{0}', false)", Base.Mask ) );
                //SetConstructionProperty( "plugins", string.Format( "[new Ext.ux.InputTextMask('{0}', false)]", Base.Mask ) );
            }
            base.Render();
        }
    }
}